package file;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/file")
public class FileCtrl {
    @PostMapping("/upload")
    @ResponseBody
    String up(MultipartFile f, HttpSession session) throws IOException {
        String dir=new SimpleDateFormat("yyyyMMdd").format(new Date());
        String path=session.getServletContext().getRealPath("/file/"+dir);
        System.out.println(path);
        File file=new File(path);
        file.mkdirs();
        //文件的名称
        String filename=f.getOriginalFilename();
        f.transferTo(new File(file,filename));

        //处理图片回显
        session.setAttribute("url","/file/"+dir+"/"+filename);
        return "upload";
    }
}
